//用户昵称
template.helper('zhName', function(name,id) {
	if(name!=""){
		return "**"+name.substr(1);
	}
	return id;
});
//转换店铺+分店
template.helper('store', function(name,branch) {
	if(branch!=""){
		return name+"("+branch+")";
	}
	return name;
});
//订单状态
template.helper('mealOrderStatus', function(status) {
	//0未支付，1已支付，-1取消，2已使用，3已转让
	var sst={
		"9":"已取消",
		"10":"未支付",
		"11":"已支付",
		"12":"已使用",
		"13":"已转让"
	};
	return sst[status+10];
});
//广告状态
template.helper('adStatus', function(status) {
	//0广告比不足，1待审核，
	var sst={
		"0":"广告币不足",
		"1":"审核中",
		"2":"已发布",
		"3":"审核不通过",
		"4":"手动停止",
		"5":"投放结束",
		"13":"已结束",
		"32":"余额不足",
		"33":"账户消耗达日限额",
		"34":"推广消耗达日限额",
	};
	return sst[status];
});
//资费类型
template.helper('costStatus', function(status) {
	//0广告比不足，1待审核，
	var sst={
		"1":"CPC(按每次点击付费)",
		"4":"CPM(按每千次曝光付费)"
	};
	return sst[status];
});
//资费类型
template.helper('constType', function(status) {
	//0广告比不足，1待审核，
	var sst={
		"1":"点击均价",
		"4":"每千次费用"
	};
	return sst[status];
});
//宝友名称
template.helper('baoyouming', function(level) {
	if(level==0){
		return "直接宝友";
	}
	if(level==1){
		return "间接宝友";
	}
	return "";
});
//处理距离
template.helper('juli', function(value) {
	value=value.replace(",","");
	if(value>1){
		return value+"km";
	}
	return value*1000+"m";
});
//格式化金额
template.helper('money', function(money) {
	var money = new Number(money)
	if(money>0){
		return money.toFixed(2);
	}
	return '0.00';
});
//奖金带随机数
template.helper('jiangjinmoney', function(money,type,min,max) {
	var money = new Number(money)
	if(money>0){
		if(type==1){
			return "随机("+(min)+"-"+(max)+"元)";
		}
		return money.toFixed(2);
	}
	return '0.00';
});
//注册模板转换器
template.helper('sexFormat', function(sex) {
	if(sex == 'F') {
		return '女';
	}
	if(sex == 'M') {
		return '男';
	}
	return '';
});

/**
 * 转换图片缩略图
 */
template.helper('convertThumb', function(picUrl) {
	return picUrl + "@180h_180w_1e_1c";
});


template.helper('convertThumb1', function(picUrl,maxWidth,maxHeight) {
	return picUrl + "?x-oss-process=image/resize,m_lfit,h_75,w_100";
});

template.helper('convertThumb2', function(picUrl,maxWidth,maxHeight) {
	if(picUrl.indexOf("http")==-1){
		if(picUrl.indexOf("/")==0){
			picUrl=picUrl.substring(1);	
		}
		picUrl="//cdn6.mofing.com/"+picUrl;
	}
	if(picUrl.indexOf("?")!=-1){
		picUrl=picUrl.split("?")[0];
	}
	return picUrl + "?x-oss-process=image/resize,m_fill,h_"+maxHeight+",w_"+maxWidth+",limit_0/auto-orient,0/quality,q_90";
});
/**
 * 默认值
 */
template.helper('default', function(str) {
	return isNullOrEmpty(str) ? "" : str;
});
template.helper('timeFormat', function(timestamp, format) {
	var date = new Date();
	date.setTime(timestamp * 1000);
	var map = {
			"M": date.getMonth() + 1, //月份 
			"d": date.getDate(), //日 
			"h": date.getHours(), //小时 
			"m": date.getMinutes(), //分 
			"s": date.getSeconds(), //秒 
			"q": Math.floor((date.getMonth() + 3) / 3), //季度 
			"S": date.getMilliseconds() //毫秒 
		};
		format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
			var v = map[t];
			if(v !== undefined) {
				if(all.length > 1) {
					v = '0' + v;
					v = v.substr(v.length - 2);
				}
				return v;
			} else if(t === 'y') {
				return(date.getFullYear() + '');
			}
			return all;
		});
		return format;
});
template.helper('dateFormat', function(date, format) {
	if(date)
	{
		//var time2 = new Date('2017/11/07 18:09:33').format("yyyy-MM-dd h:mm");
		date=date.replace("-","/").replace("-","/").replace("T"," ");
		date = new Date(date);
		var map = {
			"M": date.getMonth() + 1, //月份 
			"d": date.getDate(), //日 
			"h": date.getHours(), //小时 
			"m": date.getMinutes(), //分 
			"s": date.getSeconds(), //秒 
			"q": Math.floor((date.getMonth() + 3) / 3), //季度 
			"S": date.getMilliseconds() //毫秒 
		};
		format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
			var v = map[t];
			if(v !== undefined) {
				if(all.length > 1) {
					v = '0' + v;
					v = v.substr(v.length - 2);
				}
				return v;
			} else if(t === 'y') {
				return(date.getFullYear() + '');
			}
			return all;
		});
		return format;
	}
	else
	{
		return "";
	}
});

//从对象数组中根据对象key提取出一个新的数组
template.helper('keyArrayFormat', function(value, key) {
	var temp = [];
	for(var i = 0; i < value.length; i++) {
		temp.push(value[i][key]);
	}
	return temp;
});
template.helper('joinFormat', function(value, split) {
	if(!split) {
		split = ',';
	}
	return value.join(split);
});

//编码中文
template.helper('changeCode', function(value) {	
	return encodeURIComponent(value);
});

//千位分隔符 如:7587526变为7,587,526
template.helper('formatQian', function(value) {	
	var b = parseInt(value).toString();
	var len = b.length;
	if (len <= 3) {
		return b;
	}
	var r = len % 3;
	return r > 0 ? b.slice(0, r) + "," + b.slice(r, len).match(/\d{3}/g).join(",") : b.slice(r, len).match(/\d{3}/g).join(",");
});
